<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit"
            @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
                <el-form-item label="名称" prop="title">
                    <el-input v-model="formData.title" clearable placeholder="请输入名称" />
                </el-form-item>
                <el-form-item label="绑定模块" prop="type">
                    <el-select v-model="formData.type" clearable placeholder="请选择绑定模块">
                        <el-option v-for="item in typeList" :key="item.key" :label="item.value" :value="item.key" />
                    </el-select>
                </el-form-item>
                <el-form-item label="图片" prop="cover">
                    <material-picker v-model="formData.cover" :limit="1" />
                    <el-input v-model="formData.cover" clearable placeholder="请输入图片" :disabled="true"/>
                </el-form-item>
                <el-form-item label="跳转地址" prop="url">
                    <el-input v-model="formData.url" clearable placeholder="请输入跳转地址" />
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-input v-model="formData.status" clearable placeholder="请输入状态" />
                </el-form-item>

            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="bannerEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiBannerAdd, apiBannerEdit, apiBannerDetail, apiBannerType } from '@/api/banner'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
import { reactive, shallowRef, computed, ref, toRaw } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')


// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑首页轮播表' : '新增首页轮播表'
})


// 表单数据
const formData = reactive({
    id: '',
    title: '',
    cover: '',
    url: '',
    status: '',
    type: ''
})


// 表单验证
const formRules = reactive<any>({
    title: [{
        required: true,
        message: '请输入名称',
        trigger: ['blur']
    }],
    cover: [{
        required: true,
        message: '请输入图片',
        trigger: ['blur']
    }],
    url: [{
        required: true,
        message: '请输入跳转地址',
        trigger: ['blur']
    }],
    status: [{
        required: true,
        message: '请输入状态 1正常 0 删除',
        trigger: ['blur']
    }],
})


// 获取详情
const setFormData = async (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }


}

const getDetail = async (row: Record<string, any>) => {
    const data = await apiBannerDetail({
        id: row.id
    })
    setFormData(data)
}


// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData, }
    mode.value == 'edit'
        ? await apiBannerEdit(data)
        : await apiBannerAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
    getTypeList()
}
const typeList = ref<any[]>([])

const getTypeList = async () => {
    const data = await apiBannerType()
    // typeList.value = data
    typeList.value = Object.entries(data).map(([key, value]) => ({
        key: Number(key),
        value: String(value)
    }))

    console.log(toRaw(typeList.value))
}
// 关闭回调
const handleClose = () => {
    emit('close')
}



defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
